import { mapGetters } from "vuex";
import { getDict } from "~/utils";
import uploadMedia from "@/components/upload-media";
import {
    getLoadTree,
    getRentProperty,
    getRentPropertyRoom
} from "../api/rentalProperty.js";
let moment = require("moment");

export default {
    name: "rental_look",
    components: {
        uploadMedia
    },
    computed: {
        ...mapGetters({
            cityAssembly: "getCityAssembly",
            districtCollection: "getDistrictCollection"
        })
    },
    created() {
        this.projectId = this.$route.query.projectId;
        this.streets = getDict("streets");
        this.communityNames = getDict("community_names");
        this.manageUnits = getDict("administrative_office");
        this.landUses = getDict("land_uses");
        this.projectTypes = getDict("property_types");
        this.propertyUseTypes = getDict("property_use_types");
        this.operations = getDict("property_operations");
        this.roomUseStatus = getDict("room_use_status");
        this.planUses = getDict("plan_uses");
        this.propertySources = getDict("property_source");
        this.belongs = getDict("belongs");
        this.transferModeOption = getDict("transfer_types");
        this.handleGetLoadTree();
        this.handleGetRentProperty();
    },
    data() {
        return {
            rentalLook: [],
            defaultProps: {
                children: "children",
                label: "label"
            },
            propForm: {
                rentProjectVo: {
                    cityCode: "",
                    countyCode: "",
                    projectStreet: "",
                    communityName: "",
                    addressInfo: "",
                    mapCoordinates: "",
                    propertyName: "",
                    usedName: "",
                    totalBuildings: "",
                    buildArea: "",
                    unmanagedArea: "",
                    managedArea: "",
                    manageUnit: "",
                    floorSpace: "",
                    areaCode: "",
                    areaName: ""
                },
                rentLandVo: {
                    parcelNum: "",
                    parcelArea: "",
                    landUse: "",
                    landContractNum: "",
                    addressInfo: ""
                },
                rentPropertyVos: []
            },
            baseForm: {
                buildingName: "",
                floor: "",
                propertyName: "",
                buildingArea: "",
                projectType: "",
                projectSubclassType: "",
                operation: "",
                status: "",
                planUse: "",
                parkingSpotNum: "",
                propertySource: "",
                propertyOwnership: "",
                vrUrl: "",
                transferType: "",
                transferUnit: "",
                receiveTime: "",
                transferDesc: "",
                records: [],
                usageHistory: []
            },
            streets: [],
            communityNames: [],
            manageUnits: [],
            landUses: [],
            projectTypes: [],
            propertyUseTypes: [],
            operations: [],
            roomUseStatus: [],
            planUses: [],
            propertySources: [],
            belongs: [],
            transferModeOption: [],
            projectId: "",
            id: "",
            parentNodeFlag: true,
            isRouterAlive: true,
            fileUploadUrl: "/admin/sys-file/upLodeFileEdit",
            modelType: "rent",
            uploadFileList: []
        };
    },
    methods: {
        handleNodeClick(data) {
            this.id = data.id;
            if (data.children) {
                this.parentNodeFlag = true;
                this.handleGetRentProperty();
            } else {
                this.parentNodeFlag = false;
                this.handleGetRentPropertyRoom();
            }
        },
        // hxf-1020-回显时对应value显示label
        getCardTypeValue(num, sum) {
            let arr = sum.filter(e => e.value === num);
            if (arr.length > 0) {
                return arr[0].label;
            } else {
                return num;
            }
        },
        // hxf-1020-左边树形结构数据获取
        handleGetLoadTree() {
            getLoadTree(this.projectId).then(res => {
                if (res.data.code === 0) {
                    let nodeId = res.data.data.nodeId;
                    let label =
                        res.data.data.title +
                        "（" +
                        res.data.data.area +
                        "m²）（物业" +
                        res.data.data.childrens.length +
                        "）";
                    let children = [];
                    res.data.data.childrens.map(item => {
                        let subLable = {
                            id: item.nodeId,
                            label: item.title + "（" + item.area + "m²）"
                        };
                        children.push(subLable);
                    });
                    // 前端排序，需要时放开
                    // this.sortByLabel(children,'label')
                    let data = { id: nodeId, label: label, children: children };
                    this.rentalLook.push(data);
                }
            });
        },
        // hxf-1020-左侧父节点，右侧获取详细数据
        handleGetRentProperty() {
            getRentProperty(this.projectId).then(res => {
                if (res.data.code === 0 && res.data.data !== "") {
                    this.propForm = res.data.data;
                    this.propForm.rentProjectVo.cityCode = this.propForm.rentProjectVo.areaCode.split(
                        "-"
                    )[0];
                    this.propForm.rentProjectVo.countyCode = this.propForm.rentProjectVo.areaCode.split(
                        "-"
                    )[1];
                    this.propForm.rentPropertyVos.map((item, index) => {
                        this.propForm.rentPropertyVos[index].serialNumber =
                            index + 1;
                    });
                }
            });
        },
        // hxf-1020-左侧子节点，右侧获取详细数据
        handleGetRentPropertyRoom() {
            getRentPropertyRoom(this.id).then(res => {
                if (res.data.code === 0) {
                    this.baseForm = res.data.data;
                    this.isRouterAlive = false;
                    this.$nextTick(() => {
                        this.isRouterAlive = true;
                    });
                }
            });
        },
        // hxf-1020-关闭
        handleShutDown() {
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            this.$router.push("/wy-lessor/property-management");
        },
        // hxf-1021-物业信息时间格式化
        dataFormat(row, column, cellValue) {
            if (!cellValue) {
                return "";
            }
            return moment(cellValue).format("YYYY-MM-DD");
        },
        // hxf-1022-排序
        sortByLabel(arr, label) {
            return arr.sort((a, b) => {
                let x = a[label.split("（")[0]];
                let y = b[label.split("（")[0]];
                return x < y ? -1 : x > y ? 1 : 0;
            });
        }
    }
};
